<?php
App::import('app.Controller.AppController');
APP::import('common.Router.CRouter');
App::import('app.Vendor.UniDrive.UniDrive');
class PageController extends AppController{
	
	public $use_models = array('NodeModel');
	public $enableAuthorize = false;
	
	public function back(){
		
		CLog::getInstance()->log('Page/index', 'Welcome');
		$this->view->set('action', CRouter::url(array('controller'=>'Avatar', 'action'=>'uploadAvatar')));
		$this->view->set('goodbye', 'goodbye cc!');
		$this->view->set('its', array(1, 2, 3));
		
		echo $this->view->render();
	}
	
	public function disk(){
		
	}
	
	public function test(){
		print_r(pathinfo("/a"));
	}
	public function testinsert(){
		$this->models['NodeModel']->insert(array('name'=>'index.php', 'path'=>'/var/www/', 'dir'=>0));
	}
	
	public function testfind(){
		var_dump($this->models['NodeModel']->find());
	}
	
	public function mkdir(){
		$path = $this->request->getQuery('path');
		$d = new UniDrive();
		var_dump($d->mkdir($path));
	}
	
	public function touch(){
		$path = $this->request->getQuery('path');
		$d = new UniDrive();
		var_dump($d->touch($path));
	}
	public function ls(){
		
		$path = $this->request->getQuery('path');
		$d = new UniDrive();
		
		if(!isset($path))
			$path = '/';
		
		$nodes = $d->ls($path);
		$this->view->set('path', $path);
		$this->view->set('nodes', $nodes);
		echo $this->view->render();
		
	}
	
	public function rm(){
		$path = $this->request->getQuery('path');
		$d = new UniDrive();
		var_dump($d->rm($path));
	}
	
	public function la(){
		$d = new UniDrive();
		var_dump($d->listall());
	}
	
	public function upload(){
		
		
		
	}
	
	public function reset(){
		$d = new UniDrive();
		$d->resetFileSystem();
	}
	
	public function serverinfo(){
		print_r($_SERVER);
	}

	public function applog(){
		$params = $this->request->params;
		$date = '';
		$logDir = CLog::getInstance()->getLogDir();
		$files = scandir($logDir);
		$logs = array();
		$log2show = null;
		$path = null;
		foreach($files as $file){
			if($file == '.' || $file == '..')
				continue;
			$logs[] = substr($file, 0, strripos($file, '.'));
		}
		
		if(isset($params['query']['date'])){
			$date = $params['query']['date'];
			if(in_array($date, $logs)){
				$log2show = $date;
			}
		}
		
		if($log2show == null){
			$log2show = empty($logs) ? null : $logs[count($logs) - 1];
		}
		
		$content = '';
		if($log2show != null){
			$path = $logDir.DS.$log2show.'.log';
			$handler = fopen($path,"r");
			while (!feof($handler)) {
				$content .= fgets($handler). "<br />";
			}
			fclose($handler);
		}
		
		$this->view->set('logs', $logs);
		$this->view->set('log2show', $log2show);
		$this->view->set('content', $content);
		echo $this->view->render();
	}
}


?>